﻿namespace DymanicArray
{
    using System;
    using System.Collections.Generic;
    using System.Collections;

    public class MyList : ISuperList
    {
        class ListIterator : IEnumerator
        {
            public ListIterator(List<int> list)
            {
                m_list = list;
            }

            public bool MoveNext()
            {
                m_position = (m_callTimes % 2 == 0) ? m_list.Count - m_callTimes / 2 - 1 : m_callTimes / 2;
                m_callTimes++;
                return (m_callTimes <= m_list.Count);
            }

            public void Reset()
            {
                m_position = 0;
                m_callTimes = 0;
            }

            object IEnumerator.Current
            {
                get
                {
                    try
                    {
                        return m_list[m_position];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new IndexOutOfRangeException();
                    }
                }
            }

            #region Private Fields

            private List<int> m_list;
            private int m_callTimes = 0;
            private int m_position = 0;

            #endregion
        }

        #region Constructors

        /// <summary>
        /// Initialize new List
        /// </summary>
        public MyList()
        {
            m_list = new List<int>();
        }

        #endregion

        #region ISuperList

        /// <summary>
        /// Get amount of elements in mass
        /// </summary>
        public int Count
        {
            get { return m_list.Count; }
        }

        /// <summary>
        /// Add element at the end
        /// </summary>
        /// <param name="value">Element value</param>
        public void Add(int value)
        {
            m_list.Add(value);
        }

        /// <summary>
        /// Get list item by index
        /// </summary>
        /// <param name="index">Index value</param>
        /// <returns>Item value</returns>
        public int GetItem(int index)
        {
            if (index >= Count || index < 0)
                throw new IndexOutOfRangeException();

            return m_list[index];
        }

        /// <summary>
        /// Clears list
        /// </summary>
        public void Clear()
        {
            m_list.Clear();
        }

        public bool Compare(int[] comp)
        {
            if (comp == null)
                throw new ArgumentNullException();

            if (comp.Length != this.Count)
                return false;

            for (int i = 0; i < this.Count; i++)
            {
                if (comp[i] != GetItem(i))
                    return false;
            }

            return true;
        }

        #endregion

        #region IEnumerable implementation

        public IEnumerator GetEnumerator()
        {
            return (IEnumerator)new ListIterator(this.m_list);
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// My list
        /// </summary>
        private List<int> m_list;

        #endregion
    }
}
